package com.stgl.service;

import com.stgl.dao.LoginDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LoginService {
    @Autowired
    private LoginDao loginDao;


    public Map<String,Object>onlogin(Map<String,Object>gmap)
    {
        Map<String,Object>smap=new HashMap<String,Object>();
        //1.查询用户名是否存在
        List<Map<String,Object>>list1=loginDao.GetlistByUname(gmap);
        if(list1.size()>0){
            //2.如何用户名存在  则查询密码是否错误
            List<Map<String,Object>>list2=loginDao.GetlistByUpwd(gmap);
            if(list2.size()>0){
                smap=list2.get(0);
                smap.put("result",true);
            }else{
                smap.put("result",false);
                smap.put("msg","密码错误！");
            }
        }else{
            smap.put("result",false);
            smap.put("msg","用户名不存在！");
        }
        return smap;
    }



    public List<Map<String,Object>>getWhkStatic(String csid)
    {
        List<Map<String,Object>>list1=loginDao.getWhkStatic(csid);
        return list1;
    }

    public List<Map<String,Object>>getSthdStatic(String csid)
    {
        List<Map<String,Object>>list1=loginDao.getSthdStatic(csid);
        return list1;
    }

    public List<Map<String,Object>>getMostNewBm(String csid)
    {
        List<Map<String,Object>>list1=loginDao.getMostNewBm(csid);
        return list1;
    }

    public Map<String,Object> updateUaccount(Map<String,Object>gmap)
    {
        Map<String,Object>smap=new HashMap<String,Object>();
        //判断账号是否和其他账号重复
        List<Map<String,Object>>list1=loginDao.GetlistByUnameUsid(gmap);
        if(list1.size()>0){
            smap.put("result",false);
            smap.put("msg","账号已经存在，不能修改！");
            return smap;
        }
        //查询旧密码是否正确
        Map<String,Object>mmap=new HashMap<String,Object>();
        mmap.put("uname",gmap.get("olduname"));
        mmap.put("upwd",gmap.get("oupwd"));
        List<Map<String,Object>>list2=loginDao.GetlistByUpwd(mmap);
        if(list2.size()<=0){
            smap.put("result",false);
            smap.put("msg","密码错误！");
            return smap;
        }
        //修改
        int i=loginDao.updateUaccount(gmap);
        if(i>0){
            smap.put("result",true);
            return smap;
        }else{
            smap.put("result",false);
            smap.put("msg","修改失败！");
            return smap;
        }
    }


    public Map<String,Object>getsystemInfo(Map<String,Object>gmap)
    {
        List<Map<String,Object>>list1=loginDao.getSystemInfo(gmap);
        if(list1.size()>0){
            return list1.get(0);
        }
        return new HashMap<>();
    }


    public int updateSystemInfo(Map<String,Object>gmap)
    {
        List<Map<String,Object>>list1=loginDao.getSystemInfo(gmap);
        if(list1.size()>0){
            return loginDao.updateSystemInfo(gmap);
        }else{
            return loginDao.insertSystemInfo(gmap);
        }
    }



}
